<?php
	/**
	 * 标签类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	class tag
	{
		var $database = null;
		var $tag_file_root = '';

		/**
		 * description
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function tag(  )
		{
			$this->database = load( 'database' );
			$this->tag_file_root = config( 'dir.taglib' );
		}


		//返回全部标签定义
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 9999;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 'id asc';

			$count = 'from tag ';
			$list = 'select * ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );

			return $result;
		}


		//获取某条标签定义
		function get( $data )
		{
			$result = array();
			$where = ' where 1=1 ';

			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$where .= ' and id=' . $data['id'];
			}

			$result = $this->database->unique( 'select * from tag ' . $where );

			$result['phpsrc'] = $this->get_phpsrc( $result['class_name'] );

			return $result;
		}


		//修改标签定义
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

 			$field['phpsrc']['kind'] = '';
			$field['phpsrc']['type'] = 'text';
			$field['phpsrc']['min'] = '1';
			$field['phpsrc']['max'] = '999999';
			$field['phpsrc']['info'] = '代码不能为空';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{

				$tag_file = $this->tag_file_root . $data['class_name'] . '.php';
				if ( ! is_file( $tag_file ) )
				{
					$result['message'][] = '类文件不存在';
					return $result;
				}

				$this->database->start();
				$phpsrc = $data['phpsrc'];
				unset( $data['phpsrc'] );
				$this->database->set( 'tag', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					file_put_contents( $tag_file, $phpsrc );
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}


		//添加新的标签定义
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['class_name']['kind'] = '';
			$field['class_name']['type'] = 'text';
			$field['class_name']['min'] = '1';
			$field['class_name']['max'] = '255';
			$field['class_name']['info'] = '类名称不能为空';

 			$field['default_argument']['kind'] = '';
			$field['default_argument']['type'] = 'text';
			$field['default_argument']['min'] = '1';
			$field['default_argument']['max'] = '255';
			$field['default_argument']['info'] = '参数不能为空';

 			$field['memo']['kind'] = '';
			$field['memo']['type'] = 'text';
			$field['memo']['min'] = '1';
			$field['memo']['max'] = '255';
			$field['memo']['info'] = '备注不能为空';

 			$field['phpsrc']['kind'] = '';
			$field['phpsrc']['type'] = 'text';
			$field['phpsrc']['min'] = '1';
			$field['phpsrc']['max'] = '999999';
			$field['phpsrc']['info'] = '代码不能为空';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				//检查是否重复
				$sql = "select class_name from tag where class_name='" . $data['class_name'] . "' ";
				$tmp = $this->database->unique( $sql );
				if ( isset( $tmp['class_name'] ) )
				{
					$result['message'][] = '类名重复';
					return $result;
				}

				$tag_file = $this->tag_file_root . $data['class_name'] . '.php';
				if ( is_file( $tag_file ) )
				{
					$result['message'][] = '类文件重复';
					return $result;
				}

				$effected = 0;
				$this->database->start();

				$sql = "insert into tag ( class_name, default_argument, memo ) values ( '". $data['class_name'] . "', '". $data['default_argument'] . "', '". $data['memo'] . "' ) ";
				$this->database->command( $sql );

				if ( $this->database->end() )
				{
					file_put_contents( $tag_file, $data['phpsrc'] );
					$result['success'] = true;
				}
				else
				{
					$result['message'][] = '添加失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}


		//删除具体配置的数据
		function del( $data )
		{
			$result = array();
			if ( empty( $data ) || count( $data ) <= 0 )
			{
				return $result;
			}
			$where = ' where 1=1 ';
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$where .= ' and id = ' . $data['id'];
			}

			$result = $this->database->command( 'delete from tag ' . $where );

			return $result;
		}


		/**
		 * 根据条件判断记录是否唯一
		 * detail description
		 * @param integer name
		 * @return boolean
		 */
		function is_dup( $name )
		{
			$result = $this->database->unique( "select id from tag where name='" . $name . "' " );
			return isset( $result['id'] ) ? true : false;
		}

		/**
		 * 获取标签类源代码
		 * @param string class_name
		 * @return string
		 */
		function get_phpsrc( $class_name )
		{
			$tag_file = $this->tag_file_root . $class_name  . '.php';
			if ( is_file( $tag_file ) )
			{
				return file_get_contents( $tag_file );
			}
			else
			{
				return '';
			}
		}


		/**
		 * 获取模板的标签定义
		 * @param integer tpl_id
		 * @return array
		 */
		function get_tag_define( $tpl_id )
		{
			$return = array();
			if ( empty( $tpl_id ) )
			{
				return $return;
			}

			$sql = "select t1.id, t1.tpl_id, t1.tag_id, t1.var_name, t1.argument,t2.memo, t2.class_name from tpl_tag_define t1,tag t2 where t1.tag_id=t2.id and t1.tpl_id='" . $tpl_id . "' ";
			$tmp = $this->database->select( $sql );
			$return = $tmp;

			return $return;
		}

	}
?>